/*
 * Copyright (c) 2023 iQi.Co.Ltd. All rights reserved.
 */
 
using System.Collections.Generic;
using NetProtocol.POD;
using NetProtocol.Enum;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.GamePlay;
using IQIGame.Onigao.Framework;
using Cysharp.Threading.Tasks;
using System;
using static IQIGame.Onigao.Game.MessageWrapper;

namespace NetProtocol.Client {
	/// <summary>
	/// 装备模块 
	/// module : equip
	/// </summary>
	public interface CNetEquip : INetClientModule {
		
		public ISession GetSession();
	
		Dictionary<int, INetClientModule.Package2MessageWrapper> INetClientModule.GetModuleFunction()
        {
            MessageFunction upLevelResult_Delegate = (session, wrapper) =>
            {
            	var _wrapper = (MessageWrapper<UpLevelResultData>)wrapper;
                SC_upLevelResult(session, ref _wrapper.message);
                MessageWrapperPool<UpLevelResultData>.Put(_wrapper);
            };
            MessageFunction upStarResult_Delegate = (session, wrapper) =>
            {
            	var _wrapper = (MessageWrapper<UpStarResultData>)wrapper;
                SC_upStarResult(session, ref _wrapper.message);
                MessageWrapperPool<UpStarResultData>.Put(_wrapper);
            };
            MessageFunction decomposeResult_Delegate = (session, wrapper) =>
            {
            	var _wrapper = (MessageWrapper<DecomposeResultData>)wrapper;
                SC_decomposeResult(session, ref _wrapper.message);
                MessageWrapperPool<DecomposeResultData>.Put(_wrapper);
            };
            MessageFunction rebornResult_Delegate = (session, wrapper) =>
            {
            	var _wrapper = (MessageWrapper<RebornResultData>)wrapper;
                SC_rebornResult(session, ref _wrapper.message);
                MessageWrapperPool<RebornResultData>.Put(_wrapper);
            };
            MessageFunction lockEquipResult_Delegate = (session, wrapper) =>
            {
            	var _wrapper = (MessageWrapper<LockEquipResultData>)wrapper;
                SC_lockEquipResult(session, ref _wrapper.message);
                MessageWrapperPool<LockEquipResultData>.Put(_wrapper);
            };
            MessageFunction wearSkinResult_Delegate = (session, wrapper) =>
            {
            	var _wrapper = (MessageWrapper<WearSkinResultData>)wrapper;
                SC_wearSkinResult(session, ref _wrapper.message);
                MessageWrapperPool<WearSkinResultData>.Put(_wrapper);
            };
            MessageFunction notifySkinActive_Delegate = (session, wrapper) =>
            {
            var _wrapper = (MessageWrapper<NotifySkinActiveData>)wrapper; 
#if ENABLE_NET_LOG || UNITY_EDITOR
            NetProtocolHelper.ClientLog(NetMessageId.equip_notifySkinActive, "Recv->" + _wrapper.message.ToString());
#endif
                SC_notifySkinActive(session, _wrapper.message.skinCid);
                MessageWrapperPool<NotifySkinActiveData>.Put(_wrapper);
            };
        
            Dictionary<int, INetClientModule.Package2MessageWrapper> retDic = new()
            {
                {NetMessageId.equip_upLevelResult, (session, package) => {
                	var result = MessageWrapperPool<UpLevelResultData>.Get();
                	result.Init(session, upLevelResult_Delegate);
                	result.FromPackage(package);
                	return result;
                }},
                {NetMessageId.equip_upStarResult, (session, package) => {
                	var result = MessageWrapperPool<UpStarResultData>.Get();
                	result.Init(session, upStarResult_Delegate);
                	result.FromPackage(package);
                	return result;
                }},
                {NetMessageId.equip_decomposeResult, (session, package) => {
                	var result = MessageWrapperPool<DecomposeResultData>.Get();
                	result.Init(session, decomposeResult_Delegate);
                	result.FromPackage(package);
                	return result;
                }},
                {NetMessageId.equip_rebornResult, (session, package) => {
                	var result = MessageWrapperPool<RebornResultData>.Get();
                	result.Init(session, rebornResult_Delegate);
                	result.FromPackage(package);
                	return result;
                }},
                {NetMessageId.equip_lockEquipResult, (session, package) => {
                	var result = MessageWrapperPool<LockEquipResultData>.Get();
                	result.Init(session, lockEquipResult_Delegate);
                	result.FromPackage(package);
                	return result;
                }},
                {NetMessageId.equip_wearSkinResult, (session, package) => {
                	var result = MessageWrapperPool<WearSkinResultData>.Get();
                	result.Init(session, wearSkinResult_Delegate);
                	result.FromPackage(package);
                	return result;
                }},
                {NetMessageId.equip_notifySkinActive, (session, package) => {
                	var result = MessageWrapperPool<NotifySkinActiveData>.Get();
                	result.Init(session, notifySkinActive_Delegate);
                	result.FromPackage(package);
                	return result;
                }},
            };
            return retDic;
        }
#region Client-side Interface
		/// <summary>
        ///  物品数据会在Item模块同步
        /// </summary>
		public void SC_upLevelResult(ISession session, ref UpLevelResultData message) 
		{
            _SC_upLevelResult(session, ref message);
		}
		
		public void _SC_upLevelResult(ISession session, ref UpLevelResultData message)
		{
#if ENABLE_NET_LOG || UNITY_EDITOR
            NetProtocolHelper.ClientLog(NetMessageId.equip_upLevelResult, "Recv->" + message.ToString());
#endif
			NetProtocolHelper.endCallRequest?.Invoke(NetMessageId.equip_upLevelResult, message.code);
			MsgAsyncAwaiter<UpLevelResultData>.Instance.CompleteMsgAwait(ref message);
		}
		
		/// <summary>
        /// 
        /// </summary>
		public void SC_upStarResult(ISession session, ref UpStarResultData message) 
		{
            _SC_upStarResult(session, ref message);
		}
		
		public void _SC_upStarResult(ISession session, ref UpStarResultData message)
		{
#if ENABLE_NET_LOG || UNITY_EDITOR
            NetProtocolHelper.ClientLog(NetMessageId.equip_upStarResult, "Recv->" + message.ToString());
#endif
			NetProtocolHelper.endCallRequest?.Invoke(NetMessageId.equip_upStarResult, message.code);
			MsgAsyncAwaiter<UpStarResultData>.Instance.CompleteMsgAwait(ref message);
		}
		
		/// <summary>
        /// 
        /// </summary>
		public void SC_decomposeResult(ISession session, ref DecomposeResultData message) 
		{
            _SC_decomposeResult(session, ref message);
		}
		
		public void _SC_decomposeResult(ISession session, ref DecomposeResultData message)
		{
#if ENABLE_NET_LOG || UNITY_EDITOR
            NetProtocolHelper.ClientLog(NetMessageId.equip_decomposeResult, "Recv->" + message.ToString());
#endif
			NetProtocolHelper.endCallRequest?.Invoke(NetMessageId.equip_decomposeResult, message.code);
			MsgAsyncAwaiter<DecomposeResultData>.Instance.CompleteMsgAwait(ref message);
		}
		
		/// <summary>
        /// 
        /// </summary>
		public void SC_rebornResult(ISession session, ref RebornResultData message) 
		{
            _SC_rebornResult(session, ref message);
		}
		
		public void _SC_rebornResult(ISession session, ref RebornResultData message)
		{
#if ENABLE_NET_LOG || UNITY_EDITOR
            NetProtocolHelper.ClientLog(NetMessageId.equip_rebornResult, "Recv->" + message.ToString());
#endif
			NetProtocolHelper.endCallRequest?.Invoke(NetMessageId.equip_rebornResult, message.code);
			MsgAsyncAwaiter<RebornResultData>.Instance.CompleteMsgAwait(ref message);
		}
		
		/// <summary>
        /// 
        /// </summary>
		public void SC_lockEquipResult(ISession session, ref LockEquipResultData message) 
		{
            _SC_lockEquipResult(session, ref message);
		}
		
		public void _SC_lockEquipResult(ISession session, ref LockEquipResultData message)
		{
#if ENABLE_NET_LOG || UNITY_EDITOR
            NetProtocolHelper.ClientLog(NetMessageId.equip_lockEquipResult, "Recv->" + message.ToString());
#endif
			NetProtocolHelper.endCallRequest?.Invoke(NetMessageId.equip_lockEquipResult, message.code);
			MsgAsyncAwaiter<LockEquipResultData>.Instance.CompleteMsgAwait(ref message);
		}
		
		/// <summary>
        /// 
        /// </summary>
		public void SC_wearSkinResult(ISession session, ref WearSkinResultData message) 
		{
            _SC_wearSkinResult(session, ref message);
		}
		
		public void _SC_wearSkinResult(ISession session, ref WearSkinResultData message)
		{
#if ENABLE_NET_LOG || UNITY_EDITOR
            NetProtocolHelper.ClientLog(NetMessageId.equip_wearSkinResult, "Recv->" + message.ToString());
#endif
			NetProtocolHelper.endCallRequest?.Invoke(NetMessageId.equip_wearSkinResult, message.code);
			MsgAsyncAwaiter<WearSkinResultData>.Instance.CompleteMsgAwait(ref message);
		}
		
		/// <summary>
        ///  通知解锁了装备皮肤
        /// </summary>
		protected void SC_notifySkinActive(ISession session, int skinCid);
#endregion
#region Server-side Interface
		/// <summary>
        ///  升级
        /// </summary>
    	public UniTask<UpLevelResultData> CS_upLevel(long equipId, Dictionary<int, int> items)
        {
            return _CS_upLevel(equipId, items);
        }
        
        public UniTask<UpLevelResultData> _CS_upLevel(long equipId, Dictionary<int, int> items)
        {
        	ISession session = GetSession();
        	if(session == null) return UniTask.FromResult(default(UpLevelResultData));
#if ENABLE_NET_LOG || UNITY_EDITOR
			NetProtocolHelper.ClientLog(NetMessageId.equip_upLevel, "Send->"+"Equip.CS_upLevel { "
				+ "equipId="
				+ equipId
				+ ", items="
				+ items.ToString<int, int>()
				+" }");
#endif
        	UniTask<UpLevelResultData> task = MsgAsyncAwaiter<UpLevelResultData>.Instance.AddMsgAwait();
        	session.SendPack(Pack_for_upLevel(equipId, items));
        	NetProtocolHelper.beginCallRequest?.Invoke(NetMessageId.equip_upLevelResult);
			return task;
        }
		
		public static SendPackage Pack_for_upLevel(long equipId, Dictionary<int, int> items) 
		{
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.equip_upLevel;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_long(equipId);
			_out.Write_map_int_int(items);
			return si_p;
		}
		/// <summary>
        ///  升星
        /// </summary>
    	public UniTask<UpStarResultData> CS_upStar(long equipId)
        {
            return _CS_upStar(equipId);
        }
        
        public UniTask<UpStarResultData> _CS_upStar(long equipId)
        {
        	ISession session = GetSession();
        	if(session == null) return UniTask.FromResult(default(UpStarResultData));
#if ENABLE_NET_LOG || UNITY_EDITOR
			NetProtocolHelper.ClientLog(NetMessageId.equip_upStar, "Send->"+"Equip.CS_upStar { "
				+ "equipId="
				+ equipId
				+" }");
#endif
        	UniTask<UpStarResultData> task = MsgAsyncAwaiter<UpStarResultData>.Instance.AddMsgAwait();
        	session.SendPack(Pack_for_upStar(equipId));
        	NetProtocolHelper.beginCallRequest?.Invoke(NetMessageId.equip_upStarResult);
			return task;
        }
		
		public static SendPackage Pack_for_upStar(long equipId) 
		{
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.equip_upStar;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_long(equipId);
			return si_p;
		}
		/// <summary>
        ///  分解
        /// </summary>
    	public UniTask<DecomposeResultData> CS_decompose(List<long> equipIds)
        {
            return _CS_decompose(equipIds);
        }
        
        public UniTask<DecomposeResultData> _CS_decompose(List<long> equipIds)
        {
        	ISession session = GetSession();
        	if(session == null) return UniTask.FromResult(default(DecomposeResultData));
#if ENABLE_NET_LOG || UNITY_EDITOR
			NetProtocolHelper.ClientLog(NetMessageId.equip_decompose, "Send->"+"Equip.CS_decompose { "
				+ "equipIds="
				+ equipIds.ToString<long>()
				+" }");
#endif
        	UniTask<DecomposeResultData> task = MsgAsyncAwaiter<DecomposeResultData>.Instance.AddMsgAwait();
        	session.SendPack(Pack_for_decompose(equipIds));
        	NetProtocolHelper.beginCallRequest?.Invoke(NetMessageId.equip_decomposeResult);
			return task;
        }
		
		public static SendPackage Pack_for_decompose(List<long> equipIds) 
		{
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.equip_decompose;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_list_long(equipIds);	
			return si_p;
		}
		/// <summary>
        ///  重生
        /// </summary>
    	public UniTask<RebornResultData> CS_reborn(long equipId)
        {
            return _CS_reborn(equipId);
        }
        
        public UniTask<RebornResultData> _CS_reborn(long equipId)
        {
        	ISession session = GetSession();
        	if(session == null) return UniTask.FromResult(default(RebornResultData));
#if ENABLE_NET_LOG || UNITY_EDITOR
			NetProtocolHelper.ClientLog(NetMessageId.equip_reborn, "Send->"+"Equip.CS_reborn { "
				+ "equipId="
				+ equipId
				+" }");
#endif
        	UniTask<RebornResultData> task = MsgAsyncAwaiter<RebornResultData>.Instance.AddMsgAwait();
        	session.SendPack(Pack_for_reborn(equipId));
        	NetProtocolHelper.beginCallRequest?.Invoke(NetMessageId.equip_rebornResult);
			return task;
        }
		
		public static SendPackage Pack_for_reborn(long equipId) 
		{
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.equip_reborn;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_long(equipId);
			return si_p;
		}
		/// <summary>
        ///  锁定和解锁...lock是关键字
        /// </summary>
    	public UniTask<LockEquipResultData> CS_lockEquip(long equipId, bool isLock)
        {
            return _CS_lockEquip(equipId, isLock);
        }
        
        public UniTask<LockEquipResultData> _CS_lockEquip(long equipId, bool isLock)
        {
        	ISession session = GetSession();
        	if(session == null) return UniTask.FromResult(default(LockEquipResultData));
#if ENABLE_NET_LOG || UNITY_EDITOR
			NetProtocolHelper.ClientLog(NetMessageId.equip_lockEquip, "Send->"+"Equip.CS_lockEquip { "
				+ "equipId="
				+ equipId
				+ ", isLock="
				+ isLock
				+" }");
#endif
        	UniTask<LockEquipResultData> task = MsgAsyncAwaiter<LockEquipResultData>.Instance.AddMsgAwait();
        	session.SendPack(Pack_for_lockEquip(equipId, isLock));
        	NetProtocolHelper.beginCallRequest?.Invoke(NetMessageId.equip_lockEquipResult);
			return task;
        }
		
		public static SendPackage Pack_for_lockEquip(long equipId, bool isLock) 
		{
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.equip_lockEquip;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_long(equipId);
			_out.Write_boolean(isLock);
			return si_p;
		}
		/// <summary>
        ///  skinCid为0表示脱下，脱下的时候一定要传id；equipId为0表示批量
        /// </summary>
    	public UniTask<WearSkinResultData> CS_wearSkin(int skinCid, long equipId)
        {
            return _CS_wearSkin(skinCid, equipId);
        }
        
        public UniTask<WearSkinResultData> _CS_wearSkin(int skinCid, long equipId)
        {
        	ISession session = GetSession();
        	if(session == null) return UniTask.FromResult(default(WearSkinResultData));
#if ENABLE_NET_LOG || UNITY_EDITOR
			NetProtocolHelper.ClientLog(NetMessageId.equip_wearSkin, "Send->"+"Equip.CS_wearSkin { "
				+ "skinCid="
				+ skinCid
				+ ", equipId="
				+ equipId
				+" }");
#endif
        	UniTask<WearSkinResultData> task = MsgAsyncAwaiter<WearSkinResultData>.Instance.AddMsgAwait();
        	session.SendPack(Pack_for_wearSkin(skinCid, equipId));
        	NetProtocolHelper.beginCallRequest?.Invoke(NetMessageId.equip_wearSkinResult);
			return task;
        }
		
		public static SendPackage Pack_for_wearSkin(int skinCid, long equipId) 
		{
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.equip_wearSkin;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_int(skinCid);
			_out.Write_long(equipId);
			return si_p;
		}
#endregion
#region Messages
        public struct UpLevelResultData : IReceiveMessage {
        	private int? _code;
        	public int code {get{return _code == null ? -1 : (int)_code;}private set{_code = value;}}
	        public List<ItemShowPOD> returnItems;
            public bool IsFail => _code != null && _code != 0;
            public bool IsSuccess =>_code != null && _code == 0;
            public bool IsCanceled => _code == null;
            public void FromPackage(ReceivePackage _package)
            {
                SInputStream _in = _package.InputStream;
                code = _in.HasRemaining() ? _in.Read_int() : default;
                returnItems = _in.HasRemaining() ? _in.Read_list_pod<ItemShowPOD>(ItemShowPOD.DynamicFactory) : default;
            }
            
            public int GetMessageId() {return NetMessageId.equip_upLevelResult;}
            
            public override string ToString()
            {
                return "Equip.upLevelResult"+ ", code="+ code+ ", returnItems="+ returnItems.ToString<ItemShowPOD>();
            }
        }
        public struct UpStarResultData : IReceiveMessage {
        	private int? _code;
        	public int code {get{return _code == null ? -1 : (int)_code;}private set{_code = value;}}
            public bool IsFail => _code != null && _code != 0;
            public bool IsSuccess =>_code != null && _code == 0;
            public bool IsCanceled => _code == null;
            public void FromPackage(ReceivePackage _package)
            {
                SInputStream _in = _package.InputStream;
                code = _in.HasRemaining() ? _in.Read_int() : default;
            }
            
            public int GetMessageId() {return NetMessageId.equip_upStarResult;}
            
            public override string ToString()
            {
                return "Equip.upStarResult"+ ", code="+ code;
            }
        }
        public struct DecomposeResultData : IReceiveMessage {
        	private int? _code;
        	public int code {get{return _code == null ? -1 : (int)_code;}private set{_code = value;}}
	        public List<ItemShowPOD> awardPods;
            public bool IsFail => _code != null && _code != 0;
            public bool IsSuccess =>_code != null && _code == 0;
            public bool IsCanceled => _code == null;
            public void FromPackage(ReceivePackage _package)
            {
                SInputStream _in = _package.InputStream;
                code = _in.HasRemaining() ? _in.Read_int() : default;
                awardPods = _in.HasRemaining() ? _in.Read_list_pod<ItemShowPOD>(ItemShowPOD.DynamicFactory) : default;
            }
            
            public int GetMessageId() {return NetMessageId.equip_decomposeResult;}
            
            public override string ToString()
            {
                return "Equip.decomposeResult"+ ", code="+ code+ ", awardPods="+ awardPods.ToString<ItemShowPOD>();
            }
        }
        public struct RebornResultData : IReceiveMessage {
        	private int? _code;
        	public int code {get{return _code == null ? -1 : (int)_code;}private set{_code = value;}}
	        public List<ItemShowPOD> awardPods;
            public bool IsFail => _code != null && _code != 0;
            public bool IsSuccess =>_code != null && _code == 0;
            public bool IsCanceled => _code == null;
            public void FromPackage(ReceivePackage _package)
            {
                SInputStream _in = _package.InputStream;
                code = _in.HasRemaining() ? _in.Read_int() : default;
                awardPods = _in.HasRemaining() ? _in.Read_list_pod<ItemShowPOD>(ItemShowPOD.DynamicFactory) : default;
            }
            
            public int GetMessageId() {return NetMessageId.equip_rebornResult;}
            
            public override string ToString()
            {
                return "Equip.rebornResult"+ ", code="+ code+ ", awardPods="+ awardPods.ToString<ItemShowPOD>();
            }
        }
        public struct LockEquipResultData : IReceiveMessage {
        	private int? _code;
        	public int code {get{return _code == null ? -1 : (int)_code;}private set{_code = value;}}
            public bool IsFail => _code != null && _code != 0;
            public bool IsSuccess =>_code != null && _code == 0;
            public bool IsCanceled => _code == null;
            public void FromPackage(ReceivePackage _package)
            {
                SInputStream _in = _package.InputStream;
                code = _in.HasRemaining() ? _in.Read_int() : default;
            }
            
            public int GetMessageId() {return NetMessageId.equip_lockEquipResult;}
            
            public override string ToString()
            {
                return "Equip.lockEquipResult"+ ", code="+ code;
            }
        }
        public struct WearSkinResultData : IReceiveMessage {
        	private int? _code;
        	public int code {get{return _code == null ? -1 : (int)_code;}private set{_code = value;}}
            public bool IsFail => _code != null && _code != 0;
            public bool IsSuccess =>_code != null && _code == 0;
            public bool IsCanceled => _code == null;
            public void FromPackage(ReceivePackage _package)
            {
                SInputStream _in = _package.InputStream;
                code = _in.HasRemaining() ? _in.Read_int() : default;
            }
            
            public int GetMessageId() {return NetMessageId.equip_wearSkinResult;}
            
            public override string ToString()
            {
                return "Equip.wearSkinResult"+ ", code="+ code;
            }
        }
        public struct NotifySkinActiveData : IReceiveMessage {
	        public int skinCid;
            public void FromPackage(ReceivePackage _package)
            {
                SInputStream _in = _package.InputStream;
                skinCid = _in.HasRemaining() ? _in.Read_int() : default;
            }
            
            public int GetMessageId() {return NetMessageId.equip_notifySkinActive;}
            
            public override string ToString()
            {
                return "Equip.notifySkinActive"+ ", skinCid="+ skinCid;
            }
        }
    }
#endregion
}